﻿shogunate_ai_low_positive_weight = {
	value = 2
}

shogunate_ai_medium_positive_weight = {
	value = 5
}

shogunate_ai_high_positive_weight = {
	value = 10
}

shogunate_ai_very_high_positive_weight = {
	value = 20
}

shogunate_ai_low_negative_weight = {
	value = 0.5
}

shogunate_ai_medium_negative_weight = {
	value = 0.2
}

shogunate_ai_high_negative_weight = {
	value = 0.1
}

shogunate_ai_very_high_negative_weight = {
	value = 0.05
}

shogunate_ai_control_title_value = {
	value = 0.7
}

shogunate_ai_can_start_war_value = {
	value = 10
}

shogunate_ai_start_war_value = {

	value = 1

	multiply = shogunate_ai_military_strength_weight
	multiply = shogunate_ai_personality_weight
	multiply = shogunate_ai_region_weight

}


shogunate_ai_sengoku_invasion_war_cost_value = {

	value = 0

	scope:target_title = { 
		every_in_de_jure_hierarchy = {
			limit = {
				tier = tier_county
				holder.top_liege = scope:defender
			}
			add = 100
		}
	}

	multiply = common_cb_prestige_cost_multiplier

}


shogunate_ai_total_military_strength_value = {

	value = 0

	save_temporary_scope_as = temporary_defender

	add = {
		add = max_military_strength
		add = current_military_strength
		divide = 2
	}

	every_ally = {
		limit = {
			opinion = {
				target = scope:temporary_defender
				value >= 50
			}
		}
		add = {
			add = this.max_military_strength
			add = this.current_military_strength
			divide = 2
		}
	}

	every_character_war = {
		limit = {
			save_temporary_scope_as = temporary_war
			scope:temporary_defender = { is_defender_in_war = scope:temporary_war }
		}
		every_war_attacker = {
			add = {
				add = this.max_military_strength
				add = this.current_military_strength
				divide = 2
				multiply = 2
			}
		}
	}

}


shogunate_ai_military_strength_weight = {

	value = 0

	save_temporary_scope_as = temporary_defender

	scope:attacker = {
		add = {
			add = max_military_strength
			add = current_military_strength
			divide = 2
		}
		every_ally = {
			limit = {
				opinion = {
					target = scope:attacker
					value >= 75
				}
			}
			add = {
				add = this.max_military_strength
				add = this.current_military_strength
				divide = 2
			}
		}
	}

	scope:temporary_defender = {
		if = {
			limit = {
				shogunate_ai_total_military_strength_value > 0
			}
			divide = shogunate_ai_total_military_strength_value
		}
	}

	if = {
		limit = {
			scope:attacker = {
				is_independent_ruler = no
			}
			scope:temporary_defender = {
				is_independent_ruler = yes
			}
		}
		multiply = shogunate_ai_medium_negative_weight
	}

}


shogunate_ai_personality_weight = {

	value = 1

	# Prevent counts from invading unless ambitious
	if = {
		limit = {
			scope:attacker = {
				highest_held_title_tier <= tier_county
			}
		}
		if = {
			limit = {
				scope:attacker = {
					NOT = { has_trait = ambitious }
				}
			}
			multiply = shogunate_ai_high_negative_weight
		}
	}

	# Prevent from invading small non-feudal governments unless cynical
	if = {
		limit = {
			NOT = { government_has_flag = government_is_feudal }
			sub_realm_size = 1
		}
		if = {
			limit = {
				scope:attacker = {
					NOT = { has_trait = cynical }
				}
			}
			multiply = shogunate_ai_high_negative_weight
		}
	}

}


shogunate_ai_region_weight = {

	value = 1

	# General Rule: attack neighbors 
	if = {
		limit = {
			save_temporary_scope_as = temporary_defender
			OR = {
				AND = {
					scope:attacker.highest_held_title_tier = tier_county
					scope:attacker.capital_county ?= {
						any_this_title_or_de_jure_above = {
							tier = tier_duchy
							save_temporary_scope_as = temporary_duchy
							scope:temporary_defender = {
								any_sub_realm_county = {
									target_is_de_jure_liege_or_above = scope:temporary_duchy
								}
							}
						}
					}
				}
				AND = {
					scope:attacker.highest_held_title_tier > tier_county
					scope:attacker.capital_county ?= {
						any_this_title_or_de_jure_above = {
							tier = tier_kingdom
							save_temporary_scope_as = temporary_kingdom
							scope:temporary_defender.capital_county ?= {
								any_this_title_or_de_jure_above = {
									tier = tier_kingdom
									this = scope:temporary_kingdom
								}
							}
						}
					}
				}
			}
		}
		multiply = shogunate_ai_medium_positive_weight
	}

	# Northern Oshu
	if = {
		limit = {
			shogunate_ai_capital_location_trigger = { TITLE = k_nmih_northern_oshu }
		}
		if = {
			limit = {
				shogunate_ai_target_kingdom_trigger = {
					TITLE1 = k_nmih_northern_oshu
					TITLE2 = k_nmih_southern_oshu
					TITLE3 = k_nmih_dewa
					TITLE4 = k_nmih_eastern_kanto
					TITLE5 = k_nmih_western_kanto
					TITLE6 = k_nmih_hokuriku
				}
			}
			multiply = shogunate_ai_very_high_positive_weight
		}
	}

	# Southern Oshu
	if = {
		limit = {
			shogunate_ai_capital_location_trigger = { TITLE = k_nmih_southern_oshu }
		}
		if = {
			limit = {
				shogunate_ai_target_kingdom_trigger = {
					TITLE1 = k_nmih_northern_oshu
					TITLE2 = k_nmih_southern_oshu
					TITLE3 = k_nmih_dewa
					TITLE4 = k_nmih_eastern_kanto
					TITLE5 = k_nmih_western_kanto
					TITLE6 = k_nmih_hokuriku
				}
			}
			multiply = shogunate_ai_very_high_positive_weight
		}
	}

	# Dewa
	if = {
		limit = {
			shogunate_ai_capital_location_trigger = { TITLE = k_nmih_dewa }
		}
		if = {
			limit = {
				shogunate_ai_target_kingdom_trigger = {
					TITLE1 = k_nmih_northern_oshu
					TITLE2 = k_nmih_southern_oshu
					TITLE3 = k_nmih_dewa
					TITLE4 = k_nmih_eastern_kanto
					TITLE5 = k_nmih_western_kanto
					TITLE6 = k_nmih_hokuriku
				}
			}
			multiply = shogunate_ai_very_high_positive_weight
		}
	}

	# Eastern Kanto
	if = {
		limit = {
			shogunate_ai_capital_location_trigger = { TITLE = k_nmih_eastern_kanto }
		}
		if = {
			limit = {
				shogunate_ai_target_kingdom_trigger = {
					TITLE1 = k_nmih_southern_oshu
					TITLE2 = k_nmih_eastern_kanto
					TITLE3 = k_nmih_western_kanto
					TITLE4 = k_nmih_hokuriku
					TITLE5 = k_nmih_tokai
					TITLE6 = k_nmih_chubu
				}
			}
			multiply = shogunate_ai_very_high_positive_weight
		}
	}

	# Western Kanto
	if = {
		limit = {
			shogunate_ai_capital_location_trigger = { TITLE = k_nmih_western_kanto }
		}
		if = {
			limit = {
				shogunate_ai_target_kingdom_trigger = {
					TITLE1 = k_nmih_eastern_kanto
					TITLE2 = k_nmih_western_kanto
					TITLE3 = k_nmih_tokai
					TITLE4 = k_nmih_hokuriku
					TITLE5 = k_nmih_chubu
					TITLE6 = k_nmih_northern_kinai
				}
			}
			multiply = shogunate_ai_very_high_positive_weight
		}
	}

	# Hokuriku
	if = {
		limit = {
			shogunate_ai_capital_location_trigger = { TITLE = k_nmih_hokuriku }
		}
		if = {
			limit = {
				shogunate_ai_target_kingdom_trigger = {
					TITLE1 = k_nmih_hokuriku
					TITLE2 = k_nmih_western_kanto
					TITLE3 = k_nmih_northern_kinai
					TITLE4 = k_nmih_southern_oshu
					TITLE5 = k_nmih_tokai
					TITLE6 = k_nmih_chubu
				}
			}
			multiply = shogunate_ai_very_high_positive_weight
		}
	}

	# Tokai
	if = {
		limit = {
			shogunate_ai_capital_location_trigger = { TITLE = k_nmih_tokai }
		}
		if = {
			limit = {
				shogunate_ai_target_kingdom_trigger = {
					TITLE1 = k_nmih_tokai
					TITLE2 = k_nmih_chubu
					TITLE3 = k_nmih_northern_kinai
					TITLE4 = k_nmih_western_kanto
					TITLE5 = k_nmih_hokuriku
					TITLE6 = k_nmih_southern_kinai
				}
			}
			multiply = shogunate_ai_very_high_positive_weight
		}
	}

	# Chubu
	if = {
		limit = {
			shogunate_ai_capital_location_trigger = { TITLE = k_nmih_chubu }
		}
		if = {
			limit = {
				shogunate_ai_target_kingdom_trigger = {
					TITLE1 = k_nmih_chubu
					TITLE2 = k_nmih_northern_kinai
					TITLE3 = k_nmih_southern_kinai
					TITLE4 = k_nmih_hokuriku
					TITLE5 = k_nmih_tokai
					TITLE6 = k_nmih_sanyo
				}
			}
			multiply = shogunate_ai_very_high_positive_weight
		}
	}

	# Northern Kinai
	if = {
		limit = {
			shogunate_ai_capital_location_trigger = { TITLE = k_nmih_northern_kinai }
		}
		if = {
			limit = {
				shogunate_ai_target_kingdom_trigger = {
					TITLE1 = k_nmih_northern_kinai
					TITLE2 = k_nmih_southern_kinai
					TITLE3 = k_nmih_sanyo
					TITLE4 = k_nmih_hokuriku
					TITLE5 = k_nmih_chubu
					TITLE6 = k_nmih_sanin
				}
			}
			multiply = shogunate_ai_very_high_positive_weight
		}
	}

	# Southern Kinai
	if = {
		limit = {
			shogunate_ai_capital_location_trigger = { TITLE = k_nmih_southern_kinai }
		}
		if = {
			limit = {
				shogunate_ai_target_kingdom_trigger = {
					TITLE1 = k_nmih_northern_kinai
					TITLE2 = k_nmih_southern_kinai
					TITLE3 = k_nmih_shikoku
					TITLE4 = k_nmih_hokuriku
					TITLE5 = k_nmih_chubu
					TITLE6 = k_nmih_sanyo
				}
			}
			multiply = shogunate_ai_very_high_positive_weight
		}
	}

	# Sanyo
	if = {
		limit = {
			shogunate_ai_capital_location_trigger = { TITLE = k_nmih_sanyo }
		}
		if = {
			limit = {
				shogunate_ai_target_kingdom_trigger = {
					TITLE1 = k_nmih_sanyo
					TITLE2 = k_nmih_sanin
					TITLE3 = k_nmih_northern_kinai
					TITLE4 = k_nmih_southern_kinai
					TITLE5 = k_nmih_western_chugoku
					TITLE6 = k_nmih_shikoku
				}
			}
			multiply = shogunate_ai_very_high_positive_weight
		}
	}

	# Sanin
	if = {
		limit = {
			shogunate_ai_capital_location_trigger = { TITLE = k_nmih_sanin }
		}
		if = {
			limit = {
				shogunate_ai_target_kingdom_trigger = {
					TITLE1 = k_nmih_sanyo
					TITLE2 = k_nmih_sanin
					TITLE3 = k_nmih_northern_kinai
					TITLE4 = k_nmih_southern_kinai
					TITLE5 = k_nmih_western_chugoku
					TITLE6 = k_nmih_shikoku
				}
			}
			multiply = shogunate_ai_very_high_positive_weight
		}
	}

	# Western Chugoku
	if = {
		limit = {
			shogunate_ai_capital_location_trigger = { TITLE = k_nmih_western_chugoku }
		}
		if = {
			limit = {
				shogunate_ai_target_kingdom_trigger = {
					TITLE1 = k_nmih_sanyo
					TITLE2 = k_nmih_sanin
					TITLE3 = k_nmih_western_chugoku
					TITLE4 = k_nmih_northern_kinai
					TITLE5 = k_nmih_shikoku
					TITLE6 = k_nmih_northern_kyushu
				}
			}
			multiply = shogunate_ai_very_high_positive_weight
		}
	}

	# Shikoku
	if = {
		limit = {
			shogunate_ai_capital_location_trigger = { TITLE = k_nmih_shikoku }
		}
		if = {
			limit = {
				shogunate_ai_target_kingdom_trigger = {
					TITLE1 = k_nmih_northern_kinai
					TITLE2 = k_nmih_southern_kinai
					TITLE3 = k_nmih_shikoku
					TITLE4 = k_nmih_sanyo
					TITLE5 = k_nmih_western_chugoku
					TITLE6 = k_nmih_northern_kyushu
				}
			}
			multiply = shogunate_ai_very_high_positive_weight
		}
	}

	# Northern Kyushu
	if = {
		limit = {
			shogunate_ai_capital_location_trigger = { TITLE = k_nmih_northern_kyushu }
		}
		if = {
			limit = {
				shogunate_ai_target_kingdom_trigger = {
					TITLE1 = k_nmih_northern_kyushu
					TITLE2 = k_nmih_southern_kyushu
					TITLE3 = k_nmih_western_chugoku
					TITLE4 = k_nmih_sanyo
					TITLE5 = k_nmih_shikoku
					TITLE6 = k_nmih_northern_kinai
				}
			}
			multiply = shogunate_ai_very_high_positive_weight
		}
	}

	# Southern Kyushu
	if = {
		limit = {
			shogunate_ai_capital_location_trigger = { TITLE = k_nmih_southern_kyushu }
		}
		if = {
			limit = {
				shogunate_ai_target_kingdom_trigger = {
					TITLE1 = k_nmih_northern_kyushu
					TITLE2 = k_nmih_southern_kyushu
					TITLE3 = k_nmih_western_chugoku
					TITLE4 = k_nmih_sanyo
					TITLE5 = k_nmih_shikoku
					TITLE6 = k_nmih_northern_kinai
				}
			}
			multiply = shogunate_ai_very_high_positive_weight
		}
	}

}


shogunate_ai_war_target_county_num = {

	every_in_de_jure_hierarchy = {
		limit = {
			tier = tier_county
			holder.top_liege = scope:defender
		}
		add = 1
	}

}


shogunate_ai_war_target_value = {

	value = 0

	save_temporary_scope_as = temporary_target_title

	# Minimize prestige cost in case of taking entire realm
	if = {
		limit = {
			scope:defender.primary_title.tier < scope:attacker.primary_title.tier
			OR = {
				AND = {
					scope:temporary_target_title.tier  = tier_kingdom
					scope:defender.primary_title.tier <= tier_kingdom
				}
				AND = {
					scope:temporary_target_title.tier  = tier_duchy
					scope:defender.primary_title.tier <= tier_duchy
				}
			}
		}
		add = 100	# any number except zero
		divide = shogunate_ai_war_target_county_num
	}
	# Maximize economic profit in case of taking a part of realm
	else = {
		every_in_de_jure_hierarchy = {
			limit = {
				tier = tier_county
				holder.top_liege = scope:defender
			}
			add = development_level
		}
		every_in_de_jure_hierarchy = {
			limit = {
				tier = tier_county
				holder.top_liege = scope:defender
			}
			if = {
				limit = {
					scope:defender.capital_county ?= this
				}
				multiply = shogunate_ai_very_high_positive_weight
			}
			if = {
				limit = {
					OR = {
						this = title:c_nmih_ano					# Iwami Silver Mines
						this = title:c_nmih_otori				# Sakai Trade Port
						this = title:c_nmih_mikasa				# Hakata Trade Port
					}
				}
				multiply = shogunate_ai_high_positive_weight
			}
			if = {
				limit = {
					OR = {
						this = title:c_nmih_yamanashi			# Kurokawa Gold Mines
						this = title:c_nmih_asago				# Ikuno Silver Mines
						this = title:c_nmih_minami_murayama		# Nobesawa Silver Mines
						this = title:c_nmih_sakata				# Kunitomo Gunsmiths
						this = title:c_nmih_naga				# Negoro Gunsmiths
						this = title:c_nmih_nagusa				# Saika Gunsmiths
					}
				}
				multiply = shogunate_ai_medium_positive_weight
			}
		}
	}

}
